****************************************************************************
*``The Costs and Environmental Justice Concerns of NIMBY in Solid Waste Disposal''*
* Phuong Ho (SNF-Centre for Applied Research in Norwegian School of Economics) *
*******************************************************************************
*** This file makes Table 7 and Table D8 ***********
cls
clear all 
capture log close
set more off

global mydir "C:\XYZ" // set working directory
global data "$mydir\data"
global matlabin "$mydir\matlab\input"
global result "$mydir\TEX\"
cd "$mydir\stata"
set matsize 11000
set maxvar 32700

************ TABLE 7 ************************************
local scenario "importBan importTax"
local cnty "Marin"
**************************************************************************
**** BUILD THE SAMPLE. IMPORT STATUS QUO ESTIMATES **************************
***************************************************************************
import excel "$mydir\matlab\output\nimby_statusquo.xlsx", sheet("Sheet1") firstrow clear
label var sj_q "market size"
label var pr "model implied market share"
label var Bq "model implied trash amount"
label var el "price elasticity"
label var CS "hauler's surplus"
sort TCJ
save nimby_counterfactual, replace

**************************************************************************
****** IMPORT DEMOGRAPHICS IN 3-MILE COMMUNITIES *************************
***************************************************************************
// 1/3. Import demographics for county of origin 
use $data\demographics\raw\county_demographics_2010_CA, clear
keep *geoid *name *pop *white* *black* *hispanic* *asian* *mdhhinc
foreach x of varlist _all {
	rename `x' O_`x'
} 
rename O_COname county
gen year=2010
sort year county
tempfile tmp
save `tmp', replace
use nimby_counterfactual, clear
sort year county
merge m:1 year county using `tmp'
erase `tmp'
drop if _merge==2 //_merge==1 are not 2010, _merge==2 are counties that doesn't generate trash in 2010
drop _merge
replace O_COmdhhinc=O_COmdhhinc*index2000
save nimby_counterfactual, replace

// 2/3. Import demographics for county of destination 
use $data\demographics\raw\county_demographics_2010_CA, clear
keep *geoid *name *pop *white* *black* *hispanic* *asian* *mdhhinc
foreach x of varlist _all {
	rename `x' D_`x'
} 
rename D_COname descnty
gen year=2010
sort year descnty
tempfile tmp
save `tmp', replace
use nimby_counterfactual, clear
sort year descnty
merge m:1 year descnty using `tmp'
erase `tmp'

drop if _merge==2 //_merge==1 are not 2010, _merge==2 are counties that doesn't generate trash in 2010
drop _merge
replace D_COmdhhinc=D_COmdhhinc*index2000
save nimby_counterfactual, replace

// 3/3. Import demographics for community of destination
use $data\buffer\demographics_buffer_3mile, clear
keep code year Bpop B0pop Bwhite Bblack Basian Bhispanic Bmdhhinc
local varl "white black asian hispanic"
foreach v in `varl' {
  gen B`v'_p=B`v'*100/Bpop
}
foreach x of varlist _all {
	rename `x' D_`x'
} 
rename D_code Code
rename D_year year
sort year Code
tempfile tmp
save `tmp', replace
use nimby_counterfactual, clear
sort year Code
merge m:1 year Code using `tmp'
erase `tmp'
drop if _merge==2 //_merge==1 must be 0 if analyzing at other years, _merge==2 are counties that doesn't generate trash in 2010
drop _merge
replace D_Bmdhhinc=D_Bmdhhinc*index2000 if year==2010  // adjust income to 2000 dollars
save nimby_counterfactual, replace
****************************************************************************
**** NOW, DIFFERENT SCENARIO DOES DIFFERENT CODE ****************************

use nimby_counterfactual, clear
bys year Code: gen dupj=cond(_N==1,0,_n)
bys year county: gen dupc=cond(_N==1,0,_n)
bys year descnty: gen dupd=cond(_N==1,0,_n)
bys year quarter county: gen duptc=cond(_N==1,0,_n)
save nimby_counterfactual, replace
use nimby_counterfactual, clear 

******** TABLE 7: COLUMNS 1-2: WHEN NIMBY IS IMPLEMENTED IN MARIN COUNTY *****
******************************************************************************
foreach c in `cnty' {
foreach s in `scenario' {
	if "`s'"=="importBan" {
****1. IMPORT DATA OF COUNTERFACTUAL SCENARIO ***********
import excel "$mydir\matlab\output\nimby_counterfactual_`s'_with_eqprice_`c'.xlsx", sheet("Sheet1") firstrow clear
keep year quarter county Code Cpr Cq CCS
sort year quarter county Code
tempfile tmp
save `tmp', replace
use nimby_counterfactual, clear 
replace TC=99999 if localf==0
sort year county
by year county: egen slocal=total(localf)
replace slocal=1 if slocal>0 & slocal<. // whether a county has a local facility
rename slocal havef
sort year quarter county Code
merge 1:1 year quarter county Code using `tmp', force
drop _merge
erase `tmp'
gen Nrealprice=realprice
replace Nrealprice=Nrealprice 
gen Ndridis=dridis
gen DEL_q=Cq-Bq
gen Nq=q+DEL_q/Bq*q
replace Nq=0 if DEL_q<=1e-2 & q==0
replace Nq=Cq-Bq if DEL_q>1e-2 & q==0
replace Nq=0 if localf==0
gen Dq=Nq-q

*** DISTRIBUTIONAL EFFECTS ***************************************
keep if year==2010
bys year county: egen havelocalc=total(localf)
replace havelocalc=1 if havelocalc>0 & havelocalc<.
sort year county Code
by year county Code: gen yjdup=cond(_N==1,0,_n)
by year county Code: egen yjaq=total(Bq) // receiving q at year-county level
by year county Code: egen yjoq_del=total(Bq) if localf==0
by year county Code: egen yjoq=mean(yjoq_del)
sort year county Code
by year county Code: egen yjaNq=total(Cq) // receiving q at year-county level
by year county Code: egen yjoNq_del=total(Cq) if localf==0
by year county Code: egen yjoNq=mean(yjoNq_del)
drop *_del
sort year county
by year county: egen yQ=total(Bq)
by year county: egen tcq=total(Bq)
by year county: egen yNQ=total(Cq)

*trash going to places where noone lives
by year county: egen qTo0pop_del=total(yjaq) if yjdup<=1 & D_Bpop==0
by year county: egen qTo0pop=mean(qTo0pop_del)
replace qTo0pop=0 if qTo0pop==.
gen qTo0poppc=qTo0pop/yQ*100

by year county: egen NqTo0pop_del=total(yjaNq) if yjdup<=1 & D_Bpop==0
by year county: egen NqTo0pop=mean(NqTo0pop_del)
replace NqTo0pop=0 if NqTo0pop==.
gen NqTo0poppc=NqTo0pop/yNQ*100
gen DqTo0poppc=NqTo0poppc-qTo0poppc

by year county: egen qExport_del=total(Bq) if localf==0
by year county: egen qExport=mean(qExport_del)
drop *_del
by year county: egen NqExport_del=total(Cq) if localf==0
by year county: egen NqExport=mean(NqExport_del)
drop *_del

sort year county Code quarter
local varl "white black asian hispanic"
foreach v in `varl' {
	gen q`v'=Bq/D_Bpop*D_B`v' // waste exposed to local residents at facility
	*Calculate total waste exposed as total generated in a county
	by year county: egen c_q`v'=total(q`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year county: egen c_o`v'_del=total(q`v') if localf==0 // exports
	by year county: egen c_o`v'=mean(c_o`v'_del)
	replace c_o`v'=0 if c_o`v'==. // some county does not export
	gen Pc_q`v'=c_q`v'/yQ*100 // % of generated waste disposed of at `v'
	gen Pc_o`v'=c_o`v'/yQ*100
	gen Pce_o`v'=c_o`v'/qExport*100
}
local varl "white black asian hispanic"
foreach v in `varl' {
	gen Nq`v'=Cq/D_Bpop*D_B`v' // waste exposed to local residents at facility
	*Calculate total waste exposed as total generated in a county
	by year county: egen c_Nq`v'=total(Nq`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year county: egen c_No`v'_del=total(Nq`v') if localf==0 // exports
	by year county: egen c_No`v'=mean(c_No`v'_del)
	replace c_No`v'=0 if c_No`v'==. // some county does not export
	gen Pc_Nq`v'=c_Nq`v'/yNQ*100 // % of generated waste disposed of at `v'
	gen Pc_No`v'=c_No`v'/yNQ*100
	gen Pce_No`v'=c_No`v'/NqExport*100
}
drop *_del
local varl "white black asian hispanic"
foreach v in `varl' {
	gen DPc_q`v'=Pc_Nq`v'-Pc_q`v'
	gen DPc_o`v'=Pc_No`v'-Pc_o`v'
	gen DPce_o`v'=Pce_No`v'-Pce_o`v'
}

**** % of total waste generated in CA ******************
by year: egen CAq_del=total(Bq) if desco~="oos"
by year: egen CAq=mean(CAq_del) 
by year: egen CANq_del=total(Cq) if desco~="oos"
by year: egen CANq=mean(CANq_del)
**Total intercounty in CA
bys year: egen interq_del=total(Bq) if localf==0 & j~="oos"
bys year: egen interq=mean(interq_del)
bys year: egen Ninterq_del=total(Cq) if localf==0 & j~="oos"
bys year: egen Ninterq=mean(Ninterq_del)
drop *_del

local varl "white black asian hispanic"
foreach v in `varl' {
	*Calculate total waste exposed as total generated in CA
	by year: egen CA_q`v'=total(q`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year: egen CA_o`v'_del=total(q`v') if localf==0 // exports
	by year: egen CA_o`v'=mean(CA_o`v'_del)
	gen PCA_q`v'=CA_q`v'/CAq*100 // % of generated waste disposed of at `v'
	gen PCA_o`v'=CA_o`v'/CAq*100
	gen PCAi_o`v'=CA_o`v'/interq*100
	
	by year: egen CA_Nq`v'=total(Nq`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year: egen CA_No`v'_del=total(Nq`v') if localf==0 // exports
	by year: egen CA_No`v'=mean(CA_No`v'_del)
	gen PCA_Nq`v'=CA_Nq`v'/CANq*100 // % of generated waste disposed of at `v'
	gen PCA_No`v'=CA_No`v'/CANq*100 // out-of-county as % of total q
	gen PCAi_No`v'=CA_No`v'/Ninterq*100	// out-of-county as % of interq
}
drop *_del
local varl "white black asian hispanic"
foreach v in `varl' {
	gen DPCA_q`v'=PCA_Nq`v'-PCA_q`v'
	gen DPCA_o`v'=PCA_No`v'-PCA_o`v'
	gen DPCAi_o`v'=PCAi_No`v'-PCAi_o`v'
}
by year: egen CAqto0pop_del=total(Bq) if D_Bpop==0 & desco~="oos"
by year: egen CAqto0pop=mean(CAqto0pop_del)
replace CAqto0pop=0 if CAqto0pop==.
gen CAqto0poppc=CAqto0pop/CAq*100

by year: egen NCAqto0pop_del=total(Cq) if D_Bpop==0 & desco~="oos"
by year: egen NCAqto0pop=mean(NCAqto0pop_del)
replace NCAqto0pop=0 if NCAqto0pop==.
gen NCAqto0poppc=NCAqto0pop/CAq*100
gen DCAqto0poppc=NCAqto0poppc-CAqto0poppc
drop *_del

di "County"
di "`c'"
di "This is the result of counterfactual (with price adjustment) of scenario:"
di "`s'"
di "TABLE 7. COLUMN 1. WHEN IMPORT BAN IS IMPLEMENTED IN MARIN"
sum PCA_qwhite PCA_qblack PCA_qasian PCA_qhispanic ///
	DPCA_qwhite DPCA_qblack DPCA_qasian DPCA_qhispanic ///
	PCA_owhite PCA_oblack PCA_oasian PCA_ohispanic ///
	DPCA_owhite DPCA_oblack DPCA_oasian DPCA_ohispanic ///
	CAqto0poppc DCAqto0poppc [iw=tcq] if year==2010 & dupc<=1 & havelocalc==1

} // ending "`s'"=="importBan"
******************************************************************************
************ OTHER COUNTERFACTUALS THAN IMPORT BAN ***************************
******************************************************************************
if "`s'"!="importBan" {
**** Calculate baseline level *********
****1. IMPORT DATA OF COUNTERFACTUAL SCENARIO ***********
import excel "$mydir\matlab\output\nimby_counterfactual_`s'_with_eqprice_`c'.xlsx", sheet("Sheet1") firstrow clear
keep year quarter county Code Cpr Cq CCS
sort year quarter county Code
tempfile tmp
save `tmp', replace
use nimby_counterfactual, clear 
sort year quarter county Code
merge 1:1 year quarter county Code using `tmp', force
drop _merge
erase `tmp'
**Adjust and rename realprice, dridis, diesel price depending on counterfactual************
gen Nrealprice=realprice
if "`s'"=="importTax" {
replace Nrealprice=Nrealprice*1.10  if localf==0
}
if "`s'"=="trashTax" {
replace Nrealprice=Nrealprice*1.10
}
gen Ndridis=dridis
gen DEL_q=Cq-Bq
gen Nq=q+DEL_q/Bq*q
replace Nq=0 if DEL_q<=1e-2 & q==0
replace Nq=Cq-Bq if DEL_q>1e-2 & q==0
replace Nq=0 if localf==0
gen Dq=Nq-q

*** DISTRIBUTIONAL EFFECTS ***************************************
keep if year==2010
bys year county: egen havelocalc=total(localf)
replace havelocalc=1 if havelocalc>0 & havelocalc<.
sort year county Code
by year county Code: gen yjdup=cond(_N==1,0,_n)
by year county Code: egen yjaq=total(Bq) // receiving q at year-county level
by year county Code: egen yjoq_del=total(Bq) if localf==0
by year county Code: egen yjoq=mean(yjoq_del)
sort year county Code
by year county Code: egen yjaNq=total(Cq) // receiving q at year-county level
by year county Code: egen yjoNq_del=total(Cq) if localf==0
by year county Code: egen yjoNq=mean(yjoNq_del)
drop *_del
sort year county
by year county: egen yQ=total(Bq)
by year county: egen tcq=total(Bq)
by year county: egen yNQ=total(Cq)

*trash going to places where noone lives
by year county: egen qTo0pop_del=total(yjaq) if yjdup<=1 & D_Bpop==0
by year county: egen qTo0pop=mean(qTo0pop_del)
replace qTo0pop=0 if qTo0pop==.
gen qTo0poppc=qTo0pop/yQ*100

by year county: egen NqTo0pop_del=total(yjaNq) if yjdup<=1 & D_Bpop==0
by year county: egen NqTo0pop=mean(NqTo0pop_del)
replace NqTo0pop=0 if NqTo0pop==.
gen NqTo0poppc=NqTo0pop/yNQ*100
gen DqTo0poppc=NqTo0poppc-qTo0poppc

** Testing 2nd method. Again, still be % trash of a county to its destined facilities
by year county: egen qExport_del=total(Bq) if localf==0
by year county: egen qExport=mean(qExport_del)
drop *_del
by year county: egen NqExport_del=total(Cq) if localf==0
by year county: egen NqExport=mean(NqExport_del)
drop *_del

sort year county Code quarter
local varl "white black asian hispanic"
foreach v in `varl' {
	gen q`v'=Bq/D_Bpop*D_B`v' // waste exposed to local residents at facility
	*Calculate total waste exposed as total generated in a county
	by year county: egen c_q`v'=total(q`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year county: egen c_o`v'_del=total(q`v') if localf==0 // exports
	by year county: egen c_o`v'=mean(c_o`v'_del)
	replace c_o`v'=0 if c_o`v'==. // some county does not export
	gen Pc_q`v'=c_q`v'/yQ*100 // % of generated waste disposed of at `v'
	gen Pc_o`v'=c_o`v'/yQ*100
	gen Pce_o`v'=c_o`v'/qExport*100
}
local varl "white black asian hispanic"
foreach v in `varl' {
	gen Nq`v'=Cq/D_Bpop*D_B`v' // waste exposed to local residents at facility
	*Calculate total waste exposed as total generated in a county
	by year county: egen c_Nq`v'=total(Nq`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year county: egen c_No`v'_del=total(Nq`v') if localf==0 // exports
	by year county: egen c_No`v'=mean(c_No`v'_del)
	replace c_No`v'=0 if c_No`v'==. // some county does not export
	gen Pc_Nq`v'=c_Nq`v'/yNQ*100 // % of generated waste disposed of at `v'
	gen Pc_No`v'=c_No`v'/yNQ*100
	gen Pce_No`v'=c_No`v'/NqExport*100
}
drop *_del
local varl "white black asian hispanic"
foreach v in `varl' {
	gen DPc_q`v'=Pc_Nq`v'-Pc_q`v'
	gen DPc_o`v'=Pc_No`v'-Pc_o`v'
	gen DPce_o`v'=Pce_No`v'-Pce_o`v'
}

**** % of total waste generated in CA ******************
by year: egen CAq_del=total(Bq) if desco~="oos"
by year: egen CAq=mean(CAq_del) 
by year: egen CANq_del=total(Cq) if desco~="oos"
by year: egen CANq=mean(CANq_del)
**Total intercounty in CA
bys year: egen interq_del=total(Bq) if localf==0 & j~="oos"
bys year: egen interq=mean(interq_del)
bys year: egen Ninterq_del=total(Cq) if localf==0 & j~="oos"
bys year: egen Ninterq=mean(Ninterq_del)
drop *_del

local varl "white black asian hispanic"
foreach v in `varl' {
	*Calculate total waste exposed as total generated in CA
	by year: egen CA_q`v'=total(q`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year: egen CA_o`v'_del=total(q`v') if localf==0 // exports
	by year: egen CA_o`v'=mean(CA_o`v'_del)
	gen PCA_q`v'=CA_q`v'/CAq*100 // % of generated waste disposed of at `v'
	gen PCA_o`v'=CA_o`v'/CAq*100
	gen PCAi_o`v'=CA_o`v'/interq*100
	
	by year: egen CA_Nq`v'=total(Nq`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year: egen CA_No`v'_del=total(Nq`v') if localf==0 // exports
	by year: egen CA_No`v'=mean(CA_No`v'_del)
	gen PCA_Nq`v'=CA_Nq`v'/CANq*100 // % of generated waste disposed of at `v'
	gen PCA_No`v'=CA_No`v'/CANq*100 // out-of-county as % of total q
	gen PCAi_No`v'=CA_No`v'/Ninterq*100	// out-of-county as % of interq
}
drop *_del
local varl "white black asian hispanic"
foreach v in `varl' {
	gen DPCA_q`v'=PCA_Nq`v'-PCA_q`v'
	gen DPCA_o`v'=PCA_No`v'-PCA_o`v'
	gen DPCAi_o`v'=PCAi_No`v'-PCAi_o`v'
}
by year: egen CAqto0pop_del=total(Bq) if D_Bpop==0 & desco~="oos"
by year: egen CAqto0pop=mean(CAqto0pop_del)
replace CAqto0pop=0 if CAqto0pop==.
gen CAqto0poppc=CAqto0pop/CAq*100

by year: egen NCAqto0pop_del=total(Cq) if D_Bpop==0 & desco~="oos"
by year: egen NCAqto0pop=mean(NCAqto0pop_del)
replace NCAqto0pop=0 if NCAqto0pop==.
gen NCAqto0poppc=NCAqto0pop/CAq*100
gen DCAqto0poppc=NCAqto0poppc-CAqto0poppc
drop *_del

di "County"
di "`c'"
di "This is the result of counterfactual (with price adjustment) of scenario:"
di "`s'"
di "TABLE 7. COLUMN 2. WHEN IMPORT BAN IS IMPLEMENTED IN MARIN"
sum PCA_qwhite PCA_qblack PCA_qasian PCA_qhispanic ///
	DPCA_qwhite DPCA_qblack DPCA_qasian DPCA_qhispanic ///
	PCA_owhite PCA_oblack PCA_oasian PCA_ohispanic ///
	DPCA_owhite DPCA_oblack DPCA_oasian DPCA_ohispanic ///
	CAqto0poppc DCAqto0poppc [iw=tcq] if year==2010 & dupc<=1 & havelocalc==1

 } // ending "`s'"!="importBan"
  
} // ending foreach s in `scenario'

} // ending foreach c in `county'

***************************************************************************
*************     UNIVERSAL NIMBY. COLUMNS 3-4 IN TABLE 7   ****************
**************************************************************************
foreach s in `scenario' {
	if "`s'"=="importBan" {
****1. IMPORT DATA OF COUNTERFACTUAL SCENARIO ***********
import excel "$mydir\matlab\output\nimby_counterfactual_`s'_with_eqprice.xlsx", sheet("Sheet1") firstrow clear
keep year quarter county Code Cpr Cq CCS
sort year quarter county Code
tempfile tmp
save `tmp', replace
use nimby_counterfactual, clear 
replace TC=99999 if localf==0
sort year county
by year county: egen slocal=total(localf)
replace slocal=1 if slocal>0 & slocal<. // whether a county has a local facility
rename slocal havef
sort year quarter county Code
merge 1:1 year quarter county Code using `tmp', force
drop _merge
erase `tmp'
gen Nrealprice=realprice
replace Nrealprice=Nrealprice 
gen Ndridis=dridis
gen DEL_q=Cq-Bq
gen Nq=q+DEL_q/Bq*q
replace Nq=0 if DEL_q<=1e-2 & q==0
replace Nq=Cq-Bq if DEL_q>1e-2 & q==0
replace Nq=0 if localf==0
gen Dq=Nq-q

keep if year==2010
bys year county: egen havelocalc=total(localf)
replace havelocalc=1 if havelocalc>0 & havelocalc<.
sort year county Code
by year county Code: gen yjdup=cond(_N==1,0,_n)
by year county Code: egen yjaq=total(Bq) // receiving q at year-county level
by year county Code: egen yjoq_del=total(Bq) if localf==0
by year county Code: egen yjoq=mean(yjoq_del)
sort year county Code
by year county Code: egen yjaNq=total(Cq) // receiving q at year-county level
by year county Code: egen yjoNq_del=total(Cq) if localf==0
by year county Code: egen yjoNq=mean(yjoNq_del)
drop *_del
sort year county
by year county: egen yQ=total(Bq)
by year county: egen tcq=total(Bq)
by year county: egen yNQ=total(Cq)

*trash going to places where noone lives
by year county: egen qTo0pop_del=total(yjaq) if yjdup<=1 & D_Bpop==0
by year county: egen qTo0pop=mean(qTo0pop_del)
replace qTo0pop=0 if qTo0pop==.
gen qTo0poppc=qTo0pop/yQ*100

by year county: egen NqTo0pop_del=total(yjaNq) if yjdup<=1 & D_Bpop==0
by year county: egen NqTo0pop=mean(NqTo0pop_del)
replace NqTo0pop=0 if NqTo0pop==.
gen NqTo0poppc=NqTo0pop/yNQ*100
gen DqTo0poppc=NqTo0poppc-qTo0poppc

** Testing 2nd method. Again, still be % trash of a county to its destined facilities
by year county: egen qExport_del=total(Bq) if localf==0
by year county: egen qExport=mean(qExport_del)
drop *_del
by year county: egen NqExport_del=total(Cq) if localf==0
by year county: egen NqExport=mean(NqExport_del)
drop *_del

sort year county Code quarter
local varl "white black asian hispanic"
foreach v in `varl' {
	gen q`v'=Bq/D_Bpop*D_B`v' // waste exposed to local residents at facility
	*Calculate total waste exposed as total generated in a county
	by year county: egen c_q`v'=total(q`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year county: egen c_o`v'_del=total(q`v') if localf==0 // exports
	by year county: egen c_o`v'=mean(c_o`v'_del)
	replace c_o`v'=0 if c_o`v'==. // some county does not export
	gen Pc_q`v'=c_q`v'/yQ*100 // % of generated waste disposed of at `v'
	gen Pc_o`v'=c_o`v'/yQ*100
	gen Pce_o`v'=c_o`v'/qExport*100
}
local varl "white black asian hispanic"
foreach v in `varl' {
	gen Nq`v'=Cq/D_Bpop*D_B`v' // waste exposed to local residents at facility
	*Calculate total waste exposed as total generated in a county
	by year county: egen c_Nq`v'=total(Nq`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year county: egen c_No`v'_del=total(Nq`v') if localf==0 // exports
	by year county: egen c_No`v'=mean(c_No`v'_del)
	replace c_No`v'=0 if c_No`v'==. // some county does not export
	gen Pc_Nq`v'=c_Nq`v'/yNQ*100 // % of generated waste disposed of at `v'
	gen Pc_No`v'=c_No`v'/yNQ*100
	gen Pce_No`v'=c_No`v'/NqExport*100
}
drop *_del
local varl "white black asian hispanic"
foreach v in `varl' {
	gen DPc_q`v'=Pc_Nq`v'-Pc_q`v'
	gen DPc_o`v'=Pc_No`v'-Pc_o`v'
	gen DPce_o`v'=Pce_No`v'-Pce_o`v'
}

**** % of total waste generated in CA ******************
by year: egen CAq_del=total(Bq) if desco~="oos"
by year: egen CAq=mean(CAq_del) 
by year: egen CANq_del=total(Cq) if desco~="oos"
by year: egen CANq=mean(CANq_del)
**Total intercounty in CA
bys year: egen interq_del=total(Bq) if localf==0 & j~="oos"
bys year: egen interq=mean(interq_del)
bys year: egen Ninterq_del=total(Cq) if localf==0 & j~="oos"
bys year: egen Ninterq=mean(Ninterq_del)
drop *_del

local varl "white black asian hispanic"
foreach v in `varl' {
	*Calculate total waste exposed as total generated in CA
	by year: egen CA_q`v'=total(q`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year: egen CA_o`v'_del=total(q`v') if localf==0 // exports
	by year: egen CA_o`v'=mean(CA_o`v'_del)
	gen PCA_q`v'=CA_q`v'/CAq*100 // % of generated waste disposed of at `v'
	gen PCA_o`v'=CA_o`v'/CAq*100
	gen PCAi_o`v'=CA_o`v'/interq*100
	
	by year: egen CA_Nq`v'=total(Nq`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year: egen CA_No`v'_del=total(Nq`v') if localf==0 // exports
	by year: egen CA_No`v'=mean(CA_No`v'_del)
	gen PCA_Nq`v'=CA_Nq`v'/CANq*100 // % of generated waste disposed of at `v'
	gen PCA_No`v'=CA_No`v'/CANq*100 // out-of-county as % of total q
	gen PCAi_No`v'=CA_No`v'/Ninterq*100	// out-of-county as % of interq
}
drop *_del
local varl "white black asian hispanic"
foreach v in `varl' {
	gen DPCA_q`v'=PCA_Nq`v'-PCA_q`v'
	gen DPCA_o`v'=PCA_No`v'-PCA_o`v'
	gen DPCAi_o`v'=PCAi_No`v'-PCAi_o`v'
}

by year: egen CAqto0pop_del=total(Bq) if D_Bpop==0 & desco~="oos"
by year: egen CAqto0pop=mean(CAqto0pop_del)
replace CAqto0pop=0 if CAqto0pop==.
gen CAqto0poppc=CAqto0pop/CAq*100

by year: egen NCAqto0pop_del=total(Cq) if D_Bpop==0 & desco~="oos"
by year: egen NCAqto0pop=mean(NCAqto0pop_del)
replace NCAqto0pop=0 if NCAqto0pop==.
gen NCAqto0poppc=NCAqto0pop/CAq*100
gen DCAqto0poppc=NCAqto0poppc-CAqto0poppc
drop *_del

di "This is the result of counterfactual (with price adjustment) of scenario:"
di "`s'"
di "TABLE 7. COLUMN 3. WHEN ALL COUNTIES IMPLEMENT IMPORT BANS"
sum PCA_qwhite PCA_qblack PCA_qasian PCA_qhispanic ///
	DPCA_qwhite DPCA_qblack DPCA_qasian DPCA_qhispanic ///
	PCA_owhite PCA_oblack PCA_oasian PCA_ohispanic ///
	DPCA_owhite DPCA_oblack DPCA_oasian DPCA_ohispanic ///
	CAqto0poppc DCAqto0poppc [iw=tcq] if year==2010 & dupc<=1 & havelocalc==1

} // ending "`s'"=="importBan"

******************************************************************************
************ IMPORT TAX ***************************
if "`s'"!="importBan" {
**** Calculate baseline level *********
****1. IMPORT DATA OF COUNTERFACTUAL SCENARIO ***********
import excel "$mydir\matlab\output\nimby_counterfactual_`s'_with_eqprice.xlsx", sheet("Sheet1") firstrow clear
keep year quarter county Code Cpr Cq CCS
sort year quarter county Code
tempfile tmp
save `tmp', replace
use nimby_counterfactual, clear 
sort year quarter county Code
merge 1:1 year quarter county Code using `tmp', force
drop _merge
erase `tmp'
gen Nrealprice=realprice
if "`s'"=="importTax" {
replace Nrealprice=Nrealprice*1.10  if localf==0
}
if "`s'"=="trashTax" {
replace Nrealprice=Nrealprice*1.10
}
gen Ndridis=dridis
gen DEL_q=Cq-Bq
gen Nq=q+DEL_q/Bq*q
replace Nq=0 if DEL_q<=1e-2 & q==0
replace Nq=Cq-Bq if DEL_q>1e-2 & q==0
replace Nq=0 if localf==0
gen Dq=Nq-q

*** DISTRIBUTIONAL EFFECTS  ***************************************
keep if year==2010
bys year county: egen havelocalc=total(localf)
replace havelocalc=1 if havelocalc>0 & havelocalc<.
sort year county Code
by year county Code: gen yjdup=cond(_N==1,0,_n)
by year county Code: egen yjaq=total(Bq) // receiving q at year-county level
by year county Code: egen yjoq_del=total(Bq) if localf==0
by year county Code: egen yjoq=mean(yjoq_del)
sort year county Code
by year county Code: egen yjaNq=total(Cq) // receiving q at year-county level
by year county Code: egen yjoNq_del=total(Cq) if localf==0
by year county Code: egen yjoNq=mean(yjoNq_del)
drop *_del
sort year county
by year county: egen yQ=total(Bq)
by year county: egen tcq=total(Bq)
by year county: egen yNQ=total(Cq)

*trash going to places where noone lives
by year county: egen qTo0pop_del=total(yjaq) if yjdup<=1 & D_Bpop==0
by year county: egen qTo0pop=mean(qTo0pop_del)
replace qTo0pop=0 if qTo0pop==.
gen qTo0poppc=qTo0pop/yQ*100

by year county: egen NqTo0pop_del=total(yjaNq) if yjdup<=1 & D_Bpop==0
by year county: egen NqTo0pop=mean(NqTo0pop_del)
replace NqTo0pop=0 if NqTo0pop==.
gen NqTo0poppc=NqTo0pop/yNQ*100
gen DqTo0poppc=NqTo0poppc-qTo0poppc

** Testing 2nd method. Again, still be % trash of a county to its destined facilities
by year county: egen qExport_del=total(Bq) if localf==0
by year county: egen qExport=mean(qExport_del)
drop *_del
by year county: egen NqExport_del=total(Cq) if localf==0
by year county: egen NqExport=mean(NqExport_del)
drop *_del

sort year county Code quarter
local varl "white black asian hispanic"
foreach v in `varl' {
	gen q`v'=Bq/D_Bpop*D_B`v' // waste exposed to local residents at facility
	*Calculate total waste exposed as total generated in a county
	by year county: egen c_q`v'=total(q`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year county: egen c_o`v'_del=total(q`v') if localf==0 // exports
	by year county: egen c_o`v'=mean(c_o`v'_del)
	replace c_o`v'=0 if c_o`v'==. // some county does not export
	gen Pc_q`v'=c_q`v'/yQ*100 // % of generated waste disposed of at `v'
	gen Pc_o`v'=c_o`v'/yQ*100
	gen Pce_o`v'=c_o`v'/qExport*100
}
local varl "white black asian hispanic"
foreach v in `varl' {
	gen Nq`v'=Cq/D_Bpop*D_B`v' // waste exposed to local residents at facility
	*Calculate total waste exposed as total generated in a county
	by year county: egen c_Nq`v'=total(Nq`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year county: egen c_No`v'_del=total(Nq`v') if localf==0 // exports
	by year county: egen c_No`v'=mean(c_No`v'_del)
	replace c_No`v'=0 if c_No`v'==. // some county does not export
	gen Pc_Nq`v'=c_Nq`v'/yNQ*100 // % of generated waste disposed of at `v'
	gen Pc_No`v'=c_No`v'/yNQ*100
	gen Pce_No`v'=c_No`v'/NqExport*100
}
drop *_del
local varl "white black asian hispanic"
foreach v in `varl' {
	gen DPc_q`v'=Pc_Nq`v'-Pc_q`v'
	gen DPc_o`v'=Pc_No`v'-Pc_o`v'
	gen DPce_o`v'=Pce_No`v'-Pce_o`v'
}

**** % of total waste generated in CA ******************
by year: egen CAq_del=total(Bq) if desco~="oos"
by year: egen CAq=mean(CAq_del) 
by year: egen CANq_del=total(Cq) if desco~="oos"
by year: egen CANq=mean(CANq_del)
**Total intercounty in CA
bys year: egen interq_del=total(Bq) if localf==0 & j~="oos"
bys year: egen interq=mean(interq_del)
bys year: egen Ninterq_del=total(Cq) if localf==0 & j~="oos"
bys year: egen Ninterq=mean(Ninterq_del)
drop *_del

local varl "white black asian hispanic"
foreach v in `varl' {
	*Calculate total waste exposed as total generated in CA
	by year: egen CA_q`v'=total(q`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year: egen CA_o`v'_del=total(q`v') if localf==0 // exports
	by year: egen CA_o`v'=mean(CA_o`v'_del)
	gen PCA_q`v'=CA_q`v'/CAq*100 // % of generated waste disposed of at `v'
	gen PCA_o`v'=CA_o`v'/CAq*100
	gen PCAi_o`v'=CA_o`v'/interq*100
	
	by year: egen CA_Nq`v'=total(Nq`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year: egen CA_No`v'_del=total(Nq`v') if localf==0 // exports
	by year: egen CA_No`v'=mean(CA_No`v'_del)
	gen PCA_Nq`v'=CA_Nq`v'/CANq*100 // % of generated waste disposed of at `v'
	gen PCA_No`v'=CA_No`v'/CANq*100 // out-of-county as % of total q
	gen PCAi_No`v'=CA_No`v'/Ninterq*100	// out-of-county as % of interq
}
drop *_del
local varl "white black asian hispanic"
foreach v in `varl' {
	gen DPCA_q`v'=PCA_Nq`v'-PCA_q`v'
	gen DPCA_o`v'=PCA_No`v'-PCA_o`v'
	gen DPCAi_o`v'=PCAi_No`v'-PCAi_o`v'
}

by year: egen CAqto0pop_del=total(Bq) if D_Bpop==0 & desco~="oos"
by year: egen CAqto0pop=mean(CAqto0pop_del)
replace CAqto0pop=0 if CAqto0pop==.
gen CAqto0poppc=CAqto0pop/CAq*100

by year: egen NCAqto0pop_del=total(Cq) if D_Bpop==0 & desco~="oos"
by year: egen NCAqto0pop=mean(NCAqto0pop_del)
replace NCAqto0pop=0 if NCAqto0pop==.
gen NCAqto0poppc=NCAqto0pop/CAq*100
gen DCAqto0poppc=NCAqto0poppc-CAqto0poppc
drop *_del

di "This is the result of counterfactual (with price adjustment) of scenario:"
di "`s'"
di "TABLE 7. COLUMN 4. WHEN ALL COUNTIES IMPLEMENT IMPORT TAX"
sum PCA_qwhite PCA_qblack PCA_qasian PCA_qhispanic ///
	DPCA_qwhite DPCA_qblack DPCA_qasian DPCA_qhispanic ///
	PCA_owhite PCA_oblack PCA_oasian PCA_ohispanic ///
	DPCA_owhite DPCA_oblack DPCA_oasian DPCA_ohispanic ///
	CAqto0poppc DCAqto0poppc [iw=tcq] if year==2010 & dupc<=1 & havelocalc==1

 } // ending "`s'"!="importBan"
  
} // ending foreach s in `scenario'

******************************************************************
********** TABLE D8 ******************************************
local scenario "importBan importTax"
local cnty "Solano"
foreach c in `cnty' {
foreach s in `scenario' {
	if "`s'"=="importBan" {
****1. IMPORT DATA OF COUNTERFACTUAL SCENARIO ***********
import excel "$mydir\matlab\output\nimby_counterfactual_`s'_with_eqprice_`c'.xlsx", sheet("Sheet1") firstrow clear
keep year quarter county Code Cpr Cq CCS
sort year quarter county Code
tempfile tmp
save `tmp', replace
use nimby_counterfactual, clear 
replace TC=99999 if localf==0
sort year county
by year county: egen slocal=total(localf)
replace slocal=1 if slocal>0 & slocal<. // whether a county has a local facility
rename slocal havef
sort year quarter county Code
merge 1:1 year quarter county Code using `tmp', force
drop _merge
erase `tmp'
gen Nrealprice=realprice
replace Nrealprice=Nrealprice 
gen Ndridis=dridis

gen DEL_q=Cq-Bq
gen Nq=q+DEL_q/Bq*q
replace Nq=0 if DEL_q<=1e-2 & q==0
replace Nq=Cq-Bq if DEL_q>1e-2 & q==0
replace Nq=0 if localf==0
gen Dq=Nq-q

*** DISTRIBUTIONAL EFFECTS  ***************************************
keep if year==2010
bys year county: egen havelocalc=total(localf)
replace havelocalc=1 if havelocalc>0 & havelocalc<.
sort year county Code
by year county Code: gen yjdup=cond(_N==1,0,_n)
by year county Code: egen yjaq=total(Bq) // receiving q at year-county level
by year county Code: egen yjoq_del=total(Bq) if localf==0
by year county Code: egen yjoq=mean(yjoq_del)
sort year county Code
by year county Code: egen yjaNq=total(Cq) // receiving q at year-county level
by year county Code: egen yjoNq_del=total(Cq) if localf==0
by year county Code: egen yjoNq=mean(yjoNq_del)
drop *_del
sort year county
by year county: egen yQ=total(Bq)
by year county: egen tcq=total(Bq)
by year county: egen yNQ=total(Cq)

*trash going to places where noone lives
by year county: egen qTo0pop_del=total(yjaq) if yjdup<=1 & D_Bpop==0
by year county: egen qTo0pop=mean(qTo0pop_del)
replace qTo0pop=0 if qTo0pop==.
gen qTo0poppc=qTo0pop/yQ*100

by year county: egen NqTo0pop_del=total(yjaNq) if yjdup<=1 & D_Bpop==0
by year county: egen NqTo0pop=mean(NqTo0pop_del)
replace NqTo0pop=0 if NqTo0pop==.
gen NqTo0poppc=NqTo0pop/yNQ*100
gen DqTo0poppc=NqTo0poppc-qTo0poppc

** Testing 2nd method. Again, still be % trash of a county to its destined facilities
by year county: egen qExport_del=total(Bq) if localf==0
by year county: egen qExport=mean(qExport_del)
drop *_del
by year county: egen NqExport_del=total(Cq) if localf==0
by year county: egen NqExport=mean(NqExport_del)
drop *_del

sort year county Code quarter
local varl "white black asian hispanic"
foreach v in `varl' {
	gen q`v'=Bq/D_Bpop*D_B`v' // waste exposed to local residents at facility
	*Calculate total waste exposed as total generated in a county
	by year county: egen c_q`v'=total(q`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year county: egen c_o`v'_del=total(q`v') if localf==0 // exports
	by year county: egen c_o`v'=mean(c_o`v'_del)
	replace c_o`v'=0 if c_o`v'==. // some county does not export
	gen Pc_q`v'=c_q`v'/yQ*100 // % of generated waste disposed of at `v'
	gen Pc_o`v'=c_o`v'/yQ*100
	gen Pce_o`v'=c_o`v'/qExport*100
}
local varl "white black asian hispanic"
foreach v in `varl' {
	gen Nq`v'=Cq/D_Bpop*D_B`v' // waste exposed to local residents at facility
	*Calculate total waste exposed as total generated in a county
	by year county: egen c_Nq`v'=total(Nq`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year county: egen c_No`v'_del=total(Nq`v') if localf==0 // exports
	by year county: egen c_No`v'=mean(c_No`v'_del)
	replace c_No`v'=0 if c_No`v'==. // some county does not export
	gen Pc_Nq`v'=c_Nq`v'/yNQ*100 // % of generated waste disposed of at `v'
	gen Pc_No`v'=c_No`v'/yNQ*100
	gen Pce_No`v'=c_No`v'/NqExport*100
}
drop *_del
local varl "white black asian hispanic"
foreach v in `varl' {
	gen DPc_q`v'=Pc_Nq`v'-Pc_q`v'
	gen DPc_o`v'=Pc_No`v'-Pc_o`v'
	gen DPce_o`v'=Pce_No`v'-Pce_o`v'
}

**** % of total waste generated in CA ******************
by year: egen CAq_del=total(Bq) if desco~="oos"
by year: egen CAq=mean(CAq_del) 
by year: egen CANq_del=total(Cq) if desco~="oos"
by year: egen CANq=mean(CANq_del)
**Total intercounty in CA
bys year: egen interq_del=total(Bq) if localf==0 & j~="oos"
bys year: egen interq=mean(interq_del)
bys year: egen Ninterq_del=total(Cq) if localf==0 & j~="oos"
bys year: egen Ninterq=mean(Ninterq_del)
drop *_del

local varl "white black asian hispanic"
foreach v in `varl' {
	*Calculate total waste exposed as total generated in CA
	by year: egen CA_q`v'=total(q`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year: egen CA_o`v'_del=total(q`v') if localf==0 // exports
	by year: egen CA_o`v'=mean(CA_o`v'_del)
	gen PCA_q`v'=CA_q`v'/CAq*100 // % of generated waste disposed of at `v'
	gen PCA_o`v'=CA_o`v'/CAq*100
	gen PCAi_o`v'=CA_o`v'/interq*100
	
	by year: egen CA_Nq`v'=total(Nq`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year: egen CA_No`v'_del=total(Nq`v') if localf==0 // exports
	by year: egen CA_No`v'=mean(CA_No`v'_del)
	gen PCA_Nq`v'=CA_Nq`v'/CANq*100 // % of generated waste disposed of at `v'
	gen PCA_No`v'=CA_No`v'/CANq*100 // out-of-county as % of total q
	gen PCAi_No`v'=CA_No`v'/Ninterq*100	// out-of-county as % of interq
}
drop *_del
local varl "white black asian hispanic"
foreach v in `varl' {
	gen DPCA_q`v'=PCA_Nq`v'-PCA_q`v'
	gen DPCA_o`v'=PCA_No`v'-PCA_o`v'
	gen DPCAi_o`v'=PCAi_No`v'-PCAi_o`v'
}
by year: egen CAqto0pop_del=total(Bq) if D_Bpop==0 & desco~="oos"
by year: egen CAqto0pop=mean(CAqto0pop_del)
replace CAqto0pop=0 if CAqto0pop==.
gen CAqto0poppc=CAqto0pop/CAq*100

by year: egen NCAqto0pop_del=total(Cq) if D_Bpop==0 & desco~="oos"
by year: egen NCAqto0pop=mean(NCAqto0pop_del)
replace NCAqto0pop=0 if NCAqto0pop==.
gen NCAqto0poppc=NCAqto0pop/CAq*100
gen DCAqto0poppc=NCAqto0poppc-CAqto0poppc
drop *_del

di "County"
di "`c'"
di "This is the result of counterfactual (with price adjustment) of scenario:"
di "`s'"
di "TABLE D8. COLUMN 1. WHEN SOLANO IMPLEMENTS IMPORT BAN"
sum PCA_qwhite PCA_qblack PCA_qasian PCA_qhispanic ///
	DPCA_qwhite DPCA_qblack DPCA_qasian DPCA_qhispanic ///
	PCA_owhite PCA_oblack PCA_oasian PCA_ohispanic ///
	DPCA_owhite DPCA_oblack DPCA_oasian DPCA_ohispanic ///
	CAqto0poppc DCAqto0poppc [iw=tcq] if year==2010 & dupc<=1 & havelocalc==1

} // ending "`s'"=="importBan"
******************************************************************************
************ OTHER COUNTERFACTUALS THAN IMPORT BAN ***************************
******************************************************************************
if "`s'"!="importBan" {
**** Calculate baseline level *********
****1. IMPORT DATA OF COUNTERFACTUAL SCENARIO ***********

import excel "$mydir\matlab\output\nimby_counterfactual_`s'_with_eqprice_`c'.xlsx", sheet("Sheet1") firstrow clear
keep year quarter county Code Cpr Cq CCS
sort year quarter county Code
tempfile tmp
save `tmp', replace
use nimby_counterfactual, clear 
sort year quarter county Code
merge 1:1 year quarter county Code using `tmp', force
drop _merge
erase `tmp'
**Adjust and rename realprice, dridis, diesel price depending on counterfactual************
gen Nrealprice=realprice
if "`s'"=="importTax" {
replace Nrealprice=Nrealprice*1.10  if localf==0
}
if "`s'"=="trashTax" {
replace Nrealprice=Nrealprice*1.10
}

gen Ndridis=dridis
gen DEL_q=Cq-Bq
gen Nq=q+DEL_q/Bq*q
replace Nq=0 if DEL_q<=1e-2 & q==0
replace Nq=Cq-Bq if DEL_q>1e-2 & q==0
replace Nq=0 if localf==0
gen Dq=Nq-q

*** DISTRIBUTIONAL EFFECTS  ***************************************
keep if year==2010
bys year county: egen havelocalc=total(localf)
replace havelocalc=1 if havelocalc>0 & havelocalc<.
sort year county Code
by year county Code: gen yjdup=cond(_N==1,0,_n)
by year county Code: egen yjaq=total(Bq) // receiving q at year-county level
by year county Code: egen yjoq_del=total(Bq) if localf==0
by year county Code: egen yjoq=mean(yjoq_del)
sort year county Code
by year county Code: egen yjaNq=total(Cq) // receiving q at year-county level
by year county Code: egen yjoNq_del=total(Cq) if localf==0
by year county Code: egen yjoNq=mean(yjoNq_del)
drop *_del
sort year county
by year county: egen yQ=total(Bq)
by year county: egen tcq=total(Bq)
by year county: egen yNQ=total(Cq)

*trash going to places where noone lives
by year county: egen qTo0pop_del=total(yjaq) if yjdup<=1 & D_Bpop==0
by year county: egen qTo0pop=mean(qTo0pop_del)
replace qTo0pop=0 if qTo0pop==.
gen qTo0poppc=qTo0pop/yQ*100

by year county: egen NqTo0pop_del=total(yjaNq) if yjdup<=1 & D_Bpop==0
by year county: egen NqTo0pop=mean(NqTo0pop_del)
replace NqTo0pop=0 if NqTo0pop==.
gen NqTo0poppc=NqTo0pop/yNQ*100
gen DqTo0poppc=NqTo0poppc-qTo0poppc

** Testing 2nd method. Again, still be % trash of a county to its destined facilities
by year county: egen qExport_del=total(Bq) if localf==0
by year county: egen qExport=mean(qExport_del)
drop *_del
by year county: egen NqExport_del=total(Cq) if localf==0
by year county: egen NqExport=mean(NqExport_del)
drop *_del

sort year county Code quarter
local varl "white black asian hispanic"
foreach v in `varl' {
	gen q`v'=Bq/D_Bpop*D_B`v' // waste exposed to local residents at facility
	*Calculate total waste exposed as total generated in a county
	by year county: egen c_q`v'=total(q`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year county: egen c_o`v'_del=total(q`v') if localf==0 // exports
	by year county: egen c_o`v'=mean(c_o`v'_del)
	replace c_o`v'=0 if c_o`v'==. // some county does not export
	gen Pc_q`v'=c_q`v'/yQ*100 // % of generated waste disposed of at `v'
	gen Pc_o`v'=c_o`v'/yQ*100
	gen Pce_o`v'=c_o`v'/qExport*100
}
local varl "white black asian hispanic"
foreach v in `varl' {
	gen Nq`v'=Cq/D_Bpop*D_B`v' // waste exposed to local residents at facility
	*Calculate total waste exposed as total generated in a county
	by year county: egen c_Nq`v'=total(Nq`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year county: egen c_No`v'_del=total(Nq`v') if localf==0 // exports
	by year county: egen c_No`v'=mean(c_No`v'_del)
	replace c_No`v'=0 if c_No`v'==. // some county does not export
	gen Pc_Nq`v'=c_Nq`v'/yNQ*100 // % of generated waste disposed of at `v'
	gen Pc_No`v'=c_No`v'/yNQ*100
	gen Pce_No`v'=c_No`v'/NqExport*100
}
drop *_del
local varl "white black asian hispanic"
foreach v in `varl' {
	gen DPc_q`v'=Pc_Nq`v'-Pc_q`v'
	gen DPc_o`v'=Pc_No`v'-Pc_o`v'
	gen DPce_o`v'=Pce_No`v'-Pce_o`v'
}

**** % of total waste generated in CA ******************
by year: egen CAq_del=total(Bq) if desco~="oos"
by year: egen CAq=mean(CAq_del) 
by year: egen CANq_del=total(Cq) if desco~="oos"
by year: egen CANq=mean(CANq_del)
**Total intercounty in CA
bys year: egen interq_del=total(Bq) if localf==0 & j~="oos"
bys year: egen interq=mean(interq_del)
bys year: egen Ninterq_del=total(Cq) if localf==0 & j~="oos"
bys year: egen Ninterq=mean(Ninterq_del)
drop *_del

local varl "white black asian hispanic"
foreach v in `varl' {
	*Calculate total waste exposed as total generated in CA
	by year: egen CA_q`v'=total(q`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year: egen CA_o`v'_del=total(q`v') if localf==0 // exports
	by year: egen CA_o`v'=mean(CA_o`v'_del)
	gen PCA_q`v'=CA_q`v'/CAq*100 // % of generated waste disposed of at `v'
	gen PCA_o`v'=CA_o`v'/CAq*100
	gen PCAi_o`v'=CA_o`v'/interq*100
	
	by year: egen CA_Nq`v'=total(Nq`v')  // total generated waste
	// exports. Calculate total waste exposure as exports
	by year: egen CA_No`v'_del=total(Nq`v') if localf==0 // exports
	by year: egen CA_No`v'=mean(CA_No`v'_del)
	gen PCA_Nq`v'=CA_Nq`v'/CANq*100 // % of generated waste disposed of at `v'
	gen PCA_No`v'=CA_No`v'/CANq*100 // out-of-county as % of total q
	gen PCAi_No`v'=CA_No`v'/Ninterq*100	// out-of-county as % of interq
}
drop *_del
local varl "white black asian hispanic"
foreach v in `varl' {
	gen DPCA_q`v'=PCA_Nq`v'-PCA_q`v'
	gen DPCA_o`v'=PCA_No`v'-PCA_o`v'
	gen DPCAi_o`v'=PCAi_No`v'-PCAi_o`v'
}
by year: egen CAqto0pop_del=total(Bq) if D_Bpop==0 & desco~="oos"
by year: egen CAqto0pop=mean(CAqto0pop_del)
replace CAqto0pop=0 if CAqto0pop==.
gen CAqto0poppc=CAqto0pop/CAq*100

by year: egen NCAqto0pop_del=total(Cq) if D_Bpop==0 & desco~="oos"
by year: egen NCAqto0pop=mean(NCAqto0pop_del)
replace NCAqto0pop=0 if NCAqto0pop==.
gen NCAqto0poppc=NCAqto0pop/CAq*100
gen DCAqto0poppc=NCAqto0poppc-CAqto0poppc
drop *_del

di "County"
di "`c'"
di "This is the result of counterfactual (with price adjustment) of scenario:"
di "`s'"
di "TABLE D8. COLUMN 2. WHEN SOLANO IMPLEMENTS IMPORT TAX"
sum PCA_qwhite PCA_qblack PCA_qasian PCA_qhispanic ///
	DPCA_qwhite DPCA_qblack DPCA_qasian DPCA_qhispanic ///
	PCA_owhite PCA_oblack PCA_oasian PCA_ohispanic ///
	DPCA_owhite DPCA_oblack DPCA_oasian DPCA_ohispanic ///
	CAqto0poppc DCAqto0poppc [iw=tcq] if year==2010 & dupc<=1 & havelocalc==1

 } // ending "`s'"!="importBan"
  
} // ending foreach s in `scenario'

} // ending foreach c in `county'


